#include "vlview.h"

VLView::VLView()
{
    cvNamedWindow(MAIN_WINDOW_NAME_VL, 0);
}

void VLView::close()
{
    cvDestroyWindow(MAIN_WINDOW_NAME_VL);
}

void VLView::showSettings()
{

    std::cout<<"Global settings: \n"<<
               "verbose: " << vlModel.Verbose()<<"\n"<<
               "motion threshold: " << vlModel.GMotionThreshold()<<"\n"<<
               "time switch: " << vlModel.GTimeSwitch()<<"\n\n";
    std::cout<<"available video source\n";
    QList<VideoSource> list;
    list = vlModel.VideoList();
    int i =0;
    foreach (VideoSource video, list) {
        std::cout<<"\nindex: "<<i++<<"\n"<<
                   "name: "<<qPrintable(video.sourceName)<<"\n"<<
                   "type: "<<video.sourceType<<"\n"<<
                   "source: " << qPrintable(video.source) << "\n"<<
                   "time switch: " << video.timeSwitch<<"\n"<<
                   "motion threshold: " << video.motionThreshold<<"\n";
    }
    std::cout << "\n";
}

VideoSource VLView::choosingVideoSource()
{
    std::cout<<"Please choose a video source by entere it's index: ";

    int index = 1;
    std::cin >> index;
//    std::cout<<"1 \n";

    return vlModel.getVideoSource(index);
}

void VLView::showMainImage(IplImage *img, bool bRed, bool bUsingROI, QList<CvPoint> ROI, IplImage *mask)
{
    //TODO: show main image with virtual loop's ROI
    if(bUsingROI)
    {
        if(bRed && mask != NULL)
        {
            cvAddS(img, cvScalar(-40,-40,80),img, mask);
            drawROI(img, ROI, cvScalar(0,255, 255));
        }else
        {
            drawROI(img, ROI, cvScalar(255));
        }

    }else
    {
        drawROI(img, ROI, cvScalar(0,0,255));
    }

    cvShowImage(MAIN_WINDOW_NAME_VL, img);
}

void VLView::showDebugImage(const char* name, IplImage *img)
{
    if(vlModel.Verbose())
    {
        cvShowImage(name, img);
    }
}


void VLView::drawROI(IplImage *img, QList<CvPoint> ROI, CvScalar color)
{
    if(ROI.count() > 1)
    {
        for(int i = 1; i < ROI.count(); i++)
        {
            cvDrawLine(img, ROI[i-1], ROI[i], color, 2);
        }
    }
}

IplImage* VLView::vlNot(IplImage *img)
{
    IplImage* neg = cvCloneImage(img);
    cvNot(neg, neg);
    return neg;
}
